package cc.vicp.djx314.starfood.quartz;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.support.ServletContextResource;

public class DeleteTempHeadImageFile implements ServletContextAware {
	public static final Logger logger = LoggerFactory.getLogger(DeleteTempHeadImageFile.class);

	private String webFilePath;
	private String defaultImageName;
	private ServletContext context;

	public void deleteFile() {
		if (context != null) {
			Resource resource = new ServletContextResource(context, webFilePath);
			try {
				File[] files = resource.getFile().listFiles();
				for (File file : files) {
					String fileName = file.getName();
					if (!defaultImageName.equals(fileName)) {
						file.delete();
						logger.info("删除头像缓存文件：{}", fileName);
					}
				}
			} catch (IOException e) {
				logger.error("删除用户头像图片缓存发现未知错误", e);
			}
		}
	}


	public void setWebFilePath(String webFilePath) {
		this.webFilePath = webFilePath;
	}
	public void setDefaultImageName(String defaultImageName) {
		this.defaultImageName = defaultImageName;
	}

	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
	}
}